package pers.cj.pdos.controller;


import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import pers.cj.pdos.controller.base.BaseController;
import pers.cj.pdos.controller.entity.ResponseData;
import pers.cj.pdos.controller.entity.ResponseEntity;
import pers.cj.pdos.controller.entity.ResponseList;
import pers.cj.pdos.entity.Company;
import pers.cj.pdos.entity.Transfer;
import pers.cj.pdos.service.ITransferService;
import pers.cj.pdos.utils.DataUtil;
import pers.cj.pdos.utils.Utils;
import pers.cj.pdos.vo.TransferVo;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chenj
 * @since 2019-11-04
 */
@Controller
@RequestMapping("/funds")
public class TransferController extends BaseController {
    @Autowired
    ITransferService transferService;

    @RequiresPermissions("transfer:query")
    @RequestMapping("/transfer")
    public String transfer(){
        return "transfer/transfer";
    }


    @RequiresPermissions("transfer:query")
    @RequestMapping("/transferDetail")
    public String transferDetail(Model model, String id){
        model.addAttribute("id",id);
        return "transfer/transferDetail";
    }

    @RequiresPermissions("transfer:insert")
    @RequestMapping("/transferNew")
    public String transferNew(){
        return "transfer/transferNew";
    }

    @ResponseBody
    @RequiresPermissions("transfer:query")
    @RequestMapping("/transfer/getList")
    public ResponseList getList(int page, int limit, String close, String search) {
        Company company = getCompany();

        List<TransferVo> list = transferService.listVoPage(company.getId(), page,limit,Utils.genBoolean(close),Utils.genLikeData(search));
        return ResponseList.success(transferService.count(company.getId(),Utils.genBoolean(close),Utils.genLikeData(search)),list);
    }

    @ResponseBody
    @RequiresPermissions("transfer:insert")
    @RequestMapping("/transfer/add")
    public ResponseEntity add(@RequestBody Transfer transfer) {
        if (Utils.isEmpty(transfer.getOutSettAccountId())){
            return ResponseEntity.fail("转出账户不能为空");
        }
        if (transfer.getOutDate() == null){
            return ResponseEntity.fail("转出日期不能为空");
        }

        if (Utils.isEmpty(transfer.getInSettAccountId())){
            return ResponseEntity.fail("转入账户不能为空");
        }
        if (transfer.getInDate() == null){
            return ResponseEntity.fail("到账日期不能为空");
        }
        if (Utils.isEmpty(transfer.getUserId())){
            return ResponseEntity.fail("经办人不能为空");
        }

        if (transfer.getAmt()==null){
            return ResponseEntity.fail("金额不能为空");
        }

        if (transfer.getHandleFee()==null){
            transfer.setHandleFee(new BigDecimal("0"));
        }

        transfer.setClose(false);

        Company company = getCompany();


        DataUtil.processEntity(company,transfer);
        transferService.insert(transfer);
        return ResponseEntity.success();
    }

    @ResponseBody
    @RequiresPermissions("transfer:close")
    @RequestMapping("/transfer/close")
    public ResponseEntity close(@RequestBody Transfer transfer){
        try {
            //检查是否已经关闭
            if (transferService.checkClose(transfer)){
                return ResponseEntity.fail("该单据已被关闭，不能重复关闭");
            }

            transferService.close(transfer);
            return ResponseEntity.success();
        }catch (Exception e) {
            return ResponseEntity.fail("作废失败");
        }
    }

    @ResponseBody
    @RequestMapping("/transfer/getVoById")
    public ResponseData getVoById(String id){
        return ResponseData.success(transferService.getVoById(id));
    }
}
